//
//  Table.swift
//  CooperSwift
//
//  Created by 计海峰 on 2021/11/19.
//

import Foundation

public extension UITableView {
    
    /// 获取指定高度的tableView image
    /// - Parameter maxHeight: 最大高度
    func tableContentScreenshot(with maxHeight: CGFloat = 0) -> UIImage? {
        var viewImage: UIImage?
        
        let savedContentOffset = contentOffset
        let savedFrame = frame
        
        let imageHeight = maxHeight > 0 ? maxHeight : contentSize.height
        var screensInTable = 0
        
        if (frame.size.height != 0) {
            screensInTable = Int(ceil(imageHeight / frame.size.height))
        }
        
        let sectionNum = numberOfSections
        
//        autoreleasepool {
            let imageSize = CGSize(width: frame.size.width, height: imageHeight)
            UIGraphicsBeginImageContextWithOptions(imageSize, false, UIScreen.main.scale)
            
            frame = CGRect(x: 0, y: 0, width: contentSize.width, height: imageHeight)
            
            let oldBounds = layer.bounds
            
        
            if #available(iOS 15, *) {
                //iOS 15 系统截屏需要改变tableview 的bounds
                layer.bounds = CGRect(x: oldBounds.origin.x,
                                                 y: oldBounds.origin.y,
                                                 width: contentSize.width,
                                                 height: contentSize.height)
                
                //偏移量归零
                contentOffset = CGPoint.zero
                //frame变为contentSize
                frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height)
            }
            
            
            for i in 0..<screensInTable {
                let contentOffset = CGPoint(x: CGFloat(0), y: CGFloat(i) * frame.size.height)
                setContentOffset(contentOffset, animated: false)
                
                // 隐藏应该移出屏幕的sectionHeader
                if (style == .plain) {
                    for i in 0..<sectionNum {
                        let headerRect = rect(forSection: i)
                        if (headerRect.origin.y < contentOffset.y) {
                            setupHeaderView(with: i, true)
                        }
                    }
                }
                if let context = UIGraphicsGetCurrentContext() {
                    layer.render(in: context)
                }
            }
//        }
        
        if #available(iOS 15, *) {
            layer.bounds = oldBounds
        }
        
        viewImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        if (style == .plain) {
            for i in 0..<sectionNum {
                setupHeaderView(with: i, false)
            }
        }
        
        frame = savedFrame
        setContentOffset(savedContentOffset, animated: false)
        return viewImage
    }

    
    /// 设置HeaderView的显示隐藏
    /// - Parameters:
    ///   - section: header所在的section
    ///   - isHidden: 是否隐藏
    private func setupHeaderView(with section: Int, _ isHidden: Bool) {
        var headerView1 = headerView(forSection: section)
        if nil == headerView1 {
            headerView1 = delegate?.tableView?(self, viewForHeaderInSection: section) as? UITableViewHeaderFooterView
        }
        headerView1?.isHidden = isHidden
    }
    
}

